#ifdef _DEBUG
#   include <WinSock2.h>
#   include <vld.h>
#endif

#include <KDebugClient.h>
#include <KDepends.h>
#include <KSockAddress.h>
#include <KUtility.h>
#include <KWinSock.h>
#include <conio.h>
#include <iostream>

using namespace std;

#ifdef _DEBUG
#   pragma comment(lib, "Kirnod.lib")
#else
#   pragma comment(lib, "Kirno.lib")
#endif

class Debugger : public KDebugClient {
protected:
    virtual void onDebugAttr(uint32_t backColor, uint32_t foreColor)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), WORD(foreColor));
    }
    virtual void onDebugText(const std::string &text)
    {
        if (!text.empty()) {
            if (text.back() == '\n') {
                cout << text << flush;
            } else {
                cout << text;
            }
        }
    }
    virtual void onDebugTitle(const std::string &title)
    {
        SetConsoleTitleA(title.c_str());
    }
};

int main(int argc, char **argv)
{
    if (argc > 2) {
        KWinSock winsock;
        KTcpHostAddress addr(argv[1], argv[2]);
        if (!addr.isNull()) {
            Debugger debugger;
            KNetScene scene(KUtility::numOfProcessors() * 2);
            scene.install(&debugger);
            if (scene.start()) {
                while (!_kbhit()) {
                    if (!debugger.isConnected()) {
                        debugger.connect(addr);
                    }
                    Sleep(2000);
                }
            }
        }
    }
}